package com.maxafaa.savedata;
//http://developer.android.com/guide/topics/data/data-storage.html

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

	Button btn_save;
	EditText txtfield_string, txtfield_number;
	ToggleButton tgbtn_boolean;
	TextView txtview_string_value, txtview_number_value, txtview_boolean_value;
	public static final String PREFS_NAME1 = "MyPrefsFile1";//save record 1
	public static final String PREFS_NAME2 = "MyPrefsFile2";//save record 2
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_save = (Button) MainActivity.this.findViewById(R.id.btn_save_to_rms);
        txtfield_string = (EditText)MainActivity.this.findViewById(R.id.txtfield_input_string);
        txtfield_number = (EditText)MainActivity.this.findViewById(R.id.txtfield_input_number);
        tgbtn_boolean = (ToggleButton)MainActivity.this.findViewById(R.id.togglebtn_onoff);
        txtview_string_value = (TextView) MainActivity.this.findViewById(R.id.txtview_string_value);
        txtview_number_value = (TextView) MainActivity.this.findViewById(R.id.txtview_float_value);
        txtview_boolean_value = (TextView) MainActivity.this.findViewById(R.id.txtview_boolean_value);       
        btn_save.setOnClickListener(listener_button_save);
        LoadData();
    }
   
    private View.OnClickListener listener_button_save = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {			
			SaveData();
			MainActivity.this.finish();
		}
	};
	
	private void SaveData(){
		SharedPreferences settings = getSharedPreferences(PREFS_NAME1, MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString("StringValue", txtfield_string.getText().toString());
		if(txtfield_number.getText().toString().equalsIgnoreCase(""))
			editor.putFloat("FloatValue", (float)0.0);
		else
			editor.putFloat("FloatValue", Float.valueOf(txtfield_number.getText().toString()));
		editor.commit();
		
		settings = getSharedPreferences(PREFS_NAME2, MODE_PRIVATE);
		editor = settings.edit();
		editor.putBoolean("BooleanValue", tgbtn_boolean.isChecked());
		editor.commit();
	}
	
	private void LoadData(){
		SharedPreferences settings = getSharedPreferences(PREFS_NAME1, MODE_PRIVATE);
		txtview_string_value.setText(getString(R.string.txtfield1_default_content) + settings.getString("StringValue", "ERROR"));
		txtview_number_value.setText(getString(R.string.txtfield2_default_content) + settings.getFloat("FloatValue", -1));
		
		settings = getSharedPreferences(PREFS_NAME2, MODE_PRIVATE);
		txtview_boolean_value.setText(getString(R.string.txtfield3_default_content) + settings.getBoolean("BooleanValue", false));
	}
	
	@Override
	public void onBackPressed(){
        MainActivity.this.finish();
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
